package cn.itcast.service.impl;

import cn.itcast.dao.CourierRepository;
import cn.itcast.dao.FixedAreaRepository;
import cn.itcast.dao.TakeTimeRepository;
import cn.itcast.domain.base.Courier;
import cn.itcast.domain.base.FixedArea;
import cn.itcast.domain.base.TakeTime;
import cn.itcast.service.FixedAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;

@Controller
public class FixedAreaServiceImpl implements FixedAreaService {
    @Autowired
    private FixedAreaRepository fixedAreaRepository;
    @Override
    @Transactional
    public void save(FixedArea fixedArea) {
        fixedAreaRepository.save(fixedArea);
    }

    @Override
    public Page<FixedArea> findPageData(Specification<FixedArea> specification, Pageable pageable) {
        return fixedAreaRepository.findAll(specification,pageable);
    }
  //关联courier taketime
    @Autowired
    private CourierRepository courierRepository;
    @Autowired
    private TakeTimeRepository takeTimeRepository;
    @Override
    @Transactional
    public void associationCourierToFixedArea(FixedArea fixedArea, Integer courierId, Integer takeTimeId) {
        //获得持久化对象 对持久化对象更新数据 同步更新到数据库
        FixedArea fixedAreaOne = fixedAreaRepository.findOne(fixedArea.getId());
        Courier courier = courierRepository.findOne(courierId);
        TakeTime takeTime = takeTimeRepository.findOne(takeTimeId);
        //确定关联信息维护方 然后更新数据
        fixedAreaOne.getCouriers().add(courier);
        courier.setTakeTime(takeTime);
    }
}
